function load()
{
    localStorage["solde"] = 0;
    localStorage["devise"] = "EUR";
    rafraichirAvecNouveauSolde(localStorage["solde"]);
}

function rafraichirAvecNouveauSolde(solde)
{
    $("#soldePM").text(solde);
}

function DeviseInconnueException(devise)
{
    this.getMsg = getMsg;
    function getMsg(){
        return "Devise "+devise+" inconnue";
    }
}

function MontantIllegal(montant)
{
    this.getMsg = getMsg;
    function getMsg(){
        return "Montant "+montant+" illégal";
    }
}

function ErreurPHP(erreur)
{
    this.getMsg = getMsg;
    function getMsg(){
        return "Erreur du script php: "+erreur;
    }
}


function envoyer()
{
    var devise = $("#devise").val();
    var montant = $("#montant").val();
    
    try {
        testerDevise(devise);
        testerMontant(montant);
    } catch (objet){
        return;
    }    
    
    $.ajax({
           type:"GET",
           url:"operationAction.php",
           data:"PorteMonnaie="+localStorage["solde"]+"&Devise="+localStorage["devise"]+"&operation="+$("#operation").val()+"&v1="+montant+"&c1="+devise,
           success:function(msg){
           
           var reponse = JSON.parse(msg);
           if(reponse["Etat"] == "ok")
           {
           localStorage["solde"] = reponse["Valeur"];
           rafraichirAvecNouveauSolde(localStorage["solde"]);
           }
           else
           {
           alert(reponse.Error);
           }
           },
           error:function(requete,msg){
           throw new ErreurPHP(msg);
           }
           });
}

function testerDevise(devise)
{
    if (devise != "EUR") throw new DeviseInconnueException(devise);
}

function testerMontant(montant)
{
    if (isNaN(montant)) throw new MontantIllegal(montant);
}